#include "base.h"

int main(int argc,char **argv)
{

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@///

	  //////////////////
	  // Input   ///////
	  //////////////////

	  char kappa_sea[1024];
	  int conf_ini, conf_end, jump;
	  int Nconf;
	  char dir_input[1024];
	  char dir_output[1024];

          int Nr=2;
          int Nmass=5;
          int Nmom=140;
	  sprintf(dir_input,"./s0ft/");
	  sprintf(dir_output,"./s0ft-mom/");	

	

	  ////// Parsing command line

    	  for(int i=1; i<argc; i++) {
	    if(strncmp(argv[i],"-kappa_sea",10)==0)  sscanf(argv[i],"-kappa_sea=%s",kappa_sea);
	    if(strncmp(argv[i],"-conf_ini",9)==0)  sscanf(argv[i],"-conf_ini=%d",&conf_ini);
	    if(strncmp(argv[i],"-conf_end",9)==0)  sscanf(argv[i],"-conf_end=%d",&conf_end);
	    if(strncmp(argv[i],"-jump",5)==0)  sscanf(argv[i],"-jump=%d",&jump);
	    if(strncmp(argv[i],"-Nmass",6)==0)  sscanf(argv[i],"-Nmass=%d",&Nmass);
	    if(strncmp(argv[i],"-Nmom",6)==0)  sscanf(argv[i],"-Nmom=%d",&Nmom);
	    if(strncmp(argv[i],"-Nr",3)==0)  sscanf(argv[i],"-Nr=%d",&Nr);
            if(strncmp(argv[i],"-dir_input",10)==0)  sscanf(argv[i],"-dir_input=%s",dir_input);
            if(strncmp(argv[i],"-dir_output",11)==0)  sscanf(argv[i],"-dir_output=%s",dir_output);

    	  }

	
	  Nconf=(conf_end-conf_ini)/jump+1;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//

           /////////////////////////////
	   // Read Fourier transform
	   //////////////////////////////
	
          //def
	  colorcolorspinspin ****s0FT_r=malloc(sizeof(colorcolorspinspin***)*(Nconf));
          for (int iconf=0; iconf<Nconf; iconf++) s0FT_r[iconf]=malloc(sizeof(colorcolorspinspin**)*(Nmass));
          for (int iconf=0; iconf<Nconf; iconf++)  for(int imass=0; imass<Nmass; imass++) s0FT_r[iconf][imass]=malloc(sizeof(colorcolorspinspin*)*(Nmom)); 
	  for (int iconf=0; iconf<Nconf; iconf++)  for(int imass=0; imass<Nmass; imass++) for (int imom=0; imom<Nmom; imom++) s0FT_r[iconf][imass][imom]=malloc(sizeof(colorcolorspinspin)*(Nr));
	
	  int conf_to_read;
	  FILE *fin=NULL;
	  char file_input[1024];

           printf("====================\n");
           printf("READING...\n");

           for (int r=0; r<Nr; r++)
	   {
           for (int iconf=0; iconf<Nconf; iconf++)
 	   {
	           conf_to_read=conf_ini+iconf*jump;

                   sprintf(file_input,"%s/s0ft%d_%s_%04d.out",dir_input,r,kappa_sea,conf_to_read);
                   fin=fopen(file_input,"rb+");
                   if(fin == (FILE *)NULL) {
                        fprintf(stderr,"Unable to open file %s for reading\n",file_input);
                        return EXIT_FAILURE;
                   }
	           for(int imass=0; imass<Nmass; imass++)
                   {
		   for (int imom=0; imom<Nmom; imom++)
		   {
			for (int ic1=0; ic1<3; ic1++) for (int ic2=0; ic2<3; ic2++)  for (int is1=0; is1<4; is1++) for (int is2=0; is2<4; is2++)
			fread(&s0FT_r[iconf][imass][imom][r][ic1][ic2][is1][is2],sizeof(complex),1,fin);
	           }
		   }

	   
		   fclose(fin);
           }	
   	   }
	   printf("====================\n\n\n");




     printf("REWRITING...\n");

     FILE *fout=NULL;
     char file_output[1024];

      for (int r=0; r<Nr; r++)
       {
           for (int iconf=0; iconf<Nconf; iconf++)
           {
                    conf_to_read=conf_ini+iconf*jump;

                   for (int imom=0; imom<Nmom; imom++)
		   {
	                   sprintf(file_output,"%s/s0ft%d_%s_%04d_%03d.out",dir_output,r,kappa_sea,conf_to_read,imom);
	                   fout=fopen(file_output,"w");
        	           if(fout == (FILE *)NULL) {
                	        fprintf(stderr,"Unable to open file %s for writing\n",file_output);
                        	return EXIT_FAILURE;
                   	    }
                   	for(int imass=0; imass<Nmass; imass++)
            	        {
				for (int ic1=0; ic1<3; ic1++) for (int ic2=0; ic2<3; ic2++)  for (int is1=0; is1<4; is1++) for (int is2=0; is2<4; is2++)	
		   		fwrite(&s0FT_r[iconf][imass][imom][r][ic1][ic2][is1][is2],sizeof(complex),1,fout);

		  	}
			fclose(fout);
		   }
	   }
	}

	
     printf("====================\n\n\n");


	return 0;
}
